package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.QtOption;
import com.ruoyi.system.domain.QtTk;
import com.ruoyi.system.mapper.QtOptionMapper;
import com.ruoyi.system.mapper.QtTkMapper;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class pc {
    @Resource
    QtTkMapper qtTkMapper;
    @Resource
    QtOptionMapper qtOptionMapper;

    public  void pc1()throws Exception {

//        getData();
        String url = "http://sys.ysdjypt.com/Home/OrgTestPaper?id=1586";
        String cookie = "ASP.NET_SessionId=jujlaruh5wc5224aidmdyabm;Auto_Login_User=Auto_Login_Key_Account=å¼ ä¸¹èu008eu0089&Auto_Login_Key_S=851ECECEE086B3FAF4BA1D1118293D24";
        Connection connection= Jsoup.connect(url).header("Cookie",cookie);
        Document document=connection.get();
//        System.out.println(document);
        Elements element = document.getElementsByClass("choice");
        int i=0;
        for (Element element1 : element) {
            QtTk tk=new QtTk();

//            String tg= element1.getElementsByTag("span").text();
//            System.out.println(tg);
           Elements h5=element1.getElementsByTag("h5");
//            System.out.println(h5.get(0).getElementsByTag("span").get(0).text());//题干
            String title=h5.get(0).getElementsByTag("span").get(0).text();
            int s=h5.get(0).getElementsByTag("span").size()-1;
            //System.out.println(h5.get(0).getElementsByTag("span").get(s).text());//答案
            String key=h5.get(0).getElementsByTag("span").get(s).text();
            String score=h5.get(0).getElementsByTag("input").get(1).attr("value");
            String tkid=IdUtils.randomUUID();
            tk.setTkId(tkid);
            tk.setScore(score);
            tk.setTkKey(key);
            tk.setTkTitle(title);
            tk.setDifficulty("普通");
            tk.setChapterId("cloud1");
            tk.setTkTypeId("xuanzhe1");
            qtTkMapper.insertQtTk(tk);
            System.out.println("tk"+i+tk);

//            System.out.println(h5.get(0).getElementsByTag("input").get(1).attr("value"));//分数
//            System.out.println(h5.next().get(0).getElementsByTag("li").get(0).text());
            for (Element e:h5.next().get(0).getElementsByTag("li")){
                QtOption option=new QtOption();
                option.setOptionId(IdUtils.randomUUID());
                option.setTkId(tkid);
                option.setOptionName(e.text());

                qtOptionMapper.insertQtOption(option);
                System.out.println(option);
               // System.out.println(e.text());//选项
            }
//            if (element1.childNodeSize()==5){
//
//                System.out.println(element1.childNode(1).childNode(3).attr("text"));
//
//            }
        }

    }

    public static List<Map<String,String>> getData() throws Exception {
        //爬取的网页
        String url = "http://sys.ysdjypt.com/Home/OrgTestPaper?id=1586";
        //document相当于是个js直接操作js一样必须要联网 这一步就是获取当前网页的所有元素，接下来的操作和js差不多了ASP.NET_SessionId=rfcqwboqcyz1zczdbzh5bcfo;
        Document document = Jsoup.parse(new URL(url), 3000000);
//        Document index=Jsoup.connect("登录后想获取数据的url").cookies("Auto_Login_User=Auto_Login_Key_Account=å¼ ä¸¹è��&Auto_Login_Key_S=851ECECEE086B3FAF4BA1D1118293D24").get();
        Elements element = document.getElementsByClass("search2015_cpitem");
        List<Map<String,String>> list = new ArrayList<>();
        for (Element element1 : element) {

            String title = element1.getElementsByClass("img").attr("title");
            //懒加载的方式  所以调用data-lazy-img这个属性，而不是 src
            String image = element1.getElementsByTag("img").attr("src");
            String msgUrl = element1.getElementsByTag("a").attr("href");

            //循环获取a标签中的链接
            Document documentTwo = Jsoup.parse(new URL(msgUrl), 996000000);

            Elements elementTwo = documentTwo.getElementsByClass("info2");
            Elements elementThree = documentTwo.getElementsByClass("recipe_ingredientsw");
            Elements elementFour = documentTwo.getElementsByClass("recipe_step_num");
            Elements elementFive = documentTwo.getElementsByClass("step_content");
            Elements elementSix = documentTwo.getElementsByClass("recipe_finish_box");
            Elements elementSeven = documentTwo.getElementsByClass("recipe_tips_words");


            String describe = "";
            String mainMaterial = "";
            String step = "";
            String stepUrl = "";
            String finishedProduct = "";
            String skill = "";
            for (Element element2: elementTwo){
                describe = element2.getElementsByTag("em").text()+";"+element2.getElementsByTag("strong").text();
            }
            for (Element element3: elementThree){
                mainMaterial = element3.getElementsByTag("strong").text()+element3.getElementsByTag("a").text();
            }
            List<String> listOne = new ArrayList<>();
            List<String> listTwo = new ArrayList<>();
            for (Element element4: elementFour){
                step = element4.getElementsByTag("strong").text()+element4.getElementsByTag("p").text();
                listOne.add(step);
            }
            for (Element element5: elementFive){
                stepUrl = element5.getElementsByTag("p").text()+";图片地址"+element5.getElementsByTag("img").attr("src");
                listTwo.add(stepUrl);
            }

            for (Element element6:elementSix){
                finishedProduct = element6.getElementsByTag("img").attr("title")+":"+element6.getElementsByTag("img").attr("src")+";";
            }

            for (Element element7: elementSeven){
                skill = element7.getElementsByTag("p").text();
            }
            System.out.println(title);
//            menu.setId(UUID.randomUUID().toString().replace("-",""));
//            menu.setTitle(title);
//            menu.setImg(image);
//            menu.setDescribe(describe);
//            menu.setMainMaterial(mainMaterial);
//            menu.setStep(listOne.toString()+listTwo.toString());
//            menu.setFinishedProduct(finishedProduct);
//            menu.setSkill("烹饪技巧: "+skill);
//            menuMapper.insert(menu);
        }
        Map<String,String> map = new HashMap<>();
        map.put("Jsoup","获取网页数据呀~");
        list.add(map);
        return list;
    }
}
